package com.xixi.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xixi.anno.Log;
import com.xixi.pojo.dto.ProductDTO;
import com.xixi.pojo.query.ProductQueryPage;
import com.xixi.pojo.vo.ProductVO;
import com.xixi.pojo.vo.Result;
import com.xixi.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @GetMapping("/productById/{id}")
    public Result getProductById(@PathVariable Integer id){
        ProductVO productVO = productService.getProductById(id);
        return Result.success(productVO);
    }
    @GetMapping("/getCategory")
    public Result getCategory(){
        List<String> category = productService.getCategory();
        return Result.success(category);
    }
    @GetMapping("/productList")
    public Result list(){
        List<ProductVO> list = productService.list();
        return Result.success(list);
    }
    @GetMapping("/page")
    public Result page(ProductQueryPage queryPage){
        IPage<ProductVO> page = productService.page(queryPage);
        return Result.success(page);
    }
    @GetMapping("/productListById/{id}")
    public Result productListById(@PathVariable Integer id){
        List<ProductVO> list = productService.productListById(id);
        return Result.success(list);
    }
    @Log
    @PostMapping("/insert")
    public Result insert(@RequestBody ProductDTO productDTO){
        return productService.insert(productDTO);
    }
    @Log
    @PostMapping("/publish")
    public Result publish(@RequestBody ProductDTO productDTO){
        return productService.publish(productDTO);
    }
    @PostMapping("/upload/{productId}")
    public Result uploadImages(@RequestParam("images") List<MultipartFile> files,
                               @PathVariable Integer productId) throws Exception {
        productService.uploadProductImages(productId, files);
        return Result.success("图片上传成功");
    }
    @Log
    @PutMapping("/update")
    public Result update(@RequestBody ProductDTO productDTO){
        return productService.update(productDTO);
    }
    @Log
    @DeleteMapping("/delete")
    public Result delete(@RequestParam List<Integer> ids){
        return productService.delete(ids);
    }
}
